package com.jxc.summer.controller;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jxc.summer.service.AccountService;
import com.jxc.summer.service.CompanyService;
import com.jxc.summer.util.MD5Util;
import com.jxc.summer.util.ReturnT;
import com.jxc.summer.util.UUIDUtil;

/**
 * <p>Title: AccountController<／p>
 * @author 耿瑞阳
 * @date 2017年5月7日 下午3:33:14
 */

@Controller
@RequestMapping("/accountinfo")
public class AccountController {

	@Autowired
	private AccountService accountService;
	@Autowired
	private CompanyService companyService;
	@RequestMapping
	public String index(Model model) {
		model.addAttribute("companyList",companyService.getAllList());
		return "account/account.index";
	}
	
	@RequestMapping("/pageList")
	@ResponseBody
	public Map<String, Object> pageList(@RequestParam(required = false, defaultValue = "0") int start,  
			@RequestParam(required = false, defaultValue = "10") int length,@RequestParam(required = false, defaultValue = "") String accountName,@RequestParam(required = false, defaultValue = "") String companyName) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("accountName", accountName);
		params.put("companyName", companyName);
		params.put("offset", start);
		params.put("pagesize", length);
		Map<String, Object> result = accountService.pageList(params);
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public ReturnT<String> addAccount(@RequestParam(required = true) String username,  
			@RequestParam(required = true) String password1,@RequestParam(required = true) String company,@RequestParam(required = true) String nickname,@RequestParam(required = true) String freezetime) {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("id",UUIDUtil.get32UUID());
		params.put("accountCompanyId",UUIDUtil.get32UUID());
		params.put("username", username);
		params.put("password", MD5Util.MD5(password1));
		params.put("random", password1);
		params.put("ta_random", MD5Util.MD5(password1));
		params.put("ta_nickname", nickname);
		params.put("tca_companyid", company);
		params.put("ta_createdate", new Date());
		params.put("ta_freezedate", freezetime);
		params.put("ta_errorpasswordtime", 0);
		params.put("ta_lastlogintime", "1900-01-01");
		params.put("ta_lastloginip", "");
		params.put("ta_logincount", 0);
		if(!accountService.validateAccountNickname(params)){
			return new ReturnT<String>(500, "新增账户失败！账户名已存在。");
		}
		if(!accountService.validateAccountUsername(params)){
			return new ReturnT<String>(500, "新增账户失败！用户名已存在。");
		}
		int result = accountService.addAccount(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "新增账户失败！");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public ReturnT<String> updateAccount(@RequestParam(required = true) String id,@RequestParam(required = true) String nickname,@RequestParam(required = true)String freezetime,@RequestParam(required = true)String company){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ta_id", id);
		params.put("ta_nickname", nickname);
		params.put("ta_freezedate", freezetime);
		params.put("tca_companyid", company);
		if(!accountService.validateAccountNickname(params)){
			return new ReturnT<String>(500, "更新账户失败！账户名已存在。");
		}
		int result = accountService.updateAccount(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "更新账户失败！");
		}
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public ReturnT<String> deleteAccount(@RequestParam(required = true) String id){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ta_id", id);
		int result = accountService.deleteAccount(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "删除账户失败！");
		}
	}
	
	@RequestMapping("/updatePassword")
	@ResponseBody
	public ReturnT<String> updateAccountPassword(@RequestParam(required = true) String id,@RequestParam(required = true) String oldPassword,@RequestParam(required = true)String newPassword){
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ta_id", id);
		params.put("oldta_password", MD5Util.MD5(oldPassword));
		params.put("password", MD5Util.MD5(newPassword));
		params.put("ta_random", newPassword);
		if(!accountService.validateAccountPassword(params)){
			return new ReturnT<String>(500, "输入原密码错误！");
		}
		int result = accountService.updateAccount(params);
		if(result>0){
			return ReturnT.SUCCESS;
		}else{
			return new ReturnT<String>(500, "修改密码失败！");
		}
	}
}
